package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_UNION_WAR_UNION_SCORE_RANK,desc = "过程中商会积分排名")
public class UnionWarUnionScoreRankCmd extends AbstractCommand {

    private int activityId;
    private int phase;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.UnionScoreAndRankReqMsg reqMsg = UnionWarActivityServerProto.UnionScoreAndRankReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        phase = reqMsg.getPhase();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        String unionUid = UnionWar.getUserUnionUid(activityId, player.getUserInfo());
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        //ret = UnionPhaseWarMgr.getUnionScoreRank(player, activityId, unionUid, phase);
        //上跨服
        UnionWarActivityServerProto.UnionScoreAndRankReqMsg.Builder crossReqMsg = UnionWarActivityServerProto.UnionScoreAndRankReqMsg.newBuilder();
        crossReqMsg.setActivityId(activityId);
        crossReqMsg.setPhase(phase);
        crossReqMsg.setUnionUid(GameUnionActivityGroupMgr.getUnionUid(activityId, packet.getPlayerId()));
        player.sendUnionActivityGroupPacket(Protocol.C_UNION_WAR_UNION_SCORE_RANK, crossReqMsg, activityId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionWarActivityServerProto.UnionScoreAndRankRespMsg.Builder respMsg = UnionWarActivityServerProto.UnionScoreAndRankRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            respMsg.setPhase(phase);
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_UNION_WAR_UNION_SCORE_RANK, respMsg);
        }
    }
}
